/*
 * Copyright 2012-2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.autoconfigure.template;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.Ordered;
import org.springframework.util.Assert;
import org.springframework.web.servlet.view.AbstractTemplateViewResolver;

/**
 * {@link AbstractTemplateViewResolver} 的 {@link ConfigurationProperties @ConfigurationProperties} 基类。
 *
 * @author Andy Wilkinson
 * @since 1.1.0
 */
public abstract class AbstractTemplateViewResolverProperties extends AbstractViewResolverProperties {

	/**
	 * 构建 URL 时添加到视图名之前的前缀。
	 */
	private String prefix;

	/**
	 * 构建 URL 时添加到视图名之后的后缀。
	 */
	private String suffix;

	/**
	 * 所有视图使用的 RequestContext 属性名。
	 */
	private String requestContextAttribute;

	/**
	 * 是否在与模板合并之前，将所有请求属性添加到模型中。
	 */
	private boolean exposeRequestAttributes = false;

	/**
	 * 是否在与模板合并之前，将所有 HttpSession 属性添加到模型中。
	 */
	private boolean exposeSessionAttributes = false;

	/**
	 * 是否允许 HttpServletRequest 属性覆盖（隐藏）控制器生成的同名模型属性。
	 */
	private boolean allowRequestOverride = false;

	/**
	 * 是否为 Spring 宏库暴露 RequestContext，名称为 "springMacroRequestContext"。
	 */
	private boolean exposeSpringMacroHelpers = true;

	/**
	 * 是否允许 HttpSession 属性覆盖（隐藏）控制器生成的同名模型属性。
	 */
	private boolean allowSessionOverride = false;

	protected AbstractTemplateViewResolverProperties(String defaultPrefix, String defaultSuffix) {
		this.prefix = defaultPrefix;
		this.suffix = defaultSuffix;
	}

	public String getPrefix() {
		return this.prefix;
	}

	public void setPrefix(String prefix) {
		this.prefix = prefix;
	}

	public String getSuffix() {
		return this.suffix;
	}

	public void setSuffix(String suffix) {
		this.suffix = suffix;
	}

	public String getRequestContextAttribute() {
		return this.requestContextAttribute;
	}

	public void setRequestContextAttribute(String requestContextAttribute) {
		this.requestContextAttribute = requestContextAttribute;
	}

	public boolean isExposeRequestAttributes() {
		return this.exposeRequestAttributes;
	}

	public void setExposeRequestAttributes(boolean exposeRequestAttributes) {
		this.exposeRequestAttributes = exposeRequestAttributes;
	}

	public boolean isExposeSessionAttributes() {
		return this.exposeSessionAttributes;
	}

	public void setExposeSessionAttributes(boolean exposeSessionAttributes) {
		this.exposeSessionAttributes = exposeSessionAttributes;
	}

	public boolean isAllowRequestOverride() {
		return this.allowRequestOverride;
	}

	public void setAllowRequestOverride(boolean allowRequestOverride) {
		this.allowRequestOverride = allowRequestOverride;
	}

	public boolean isAllowSessionOverride() {
		return this.allowSessionOverride;
	}

	public void setAllowSessionOverride(boolean allowSessionOverride) {
		this.allowSessionOverride = allowSessionOverride;
	}

	public boolean isExposeSpringMacroHelpers() {
		return this.exposeSpringMacroHelpers;
	}

	public void setExposeSpringMacroHelpers(boolean exposeSpringMacroHelpers) {
		this.exposeSpringMacroHelpers = exposeSpringMacroHelpers;
	}

	/**
	 * 将给定属性应用于 {@link AbstractTemplateViewResolver}。
	 * 使用 Object 作为参数类型以避免对 MVC 的运行时依赖，
	 * 这意味着模板引擎可以在非 Web 应用中使用。
	 * @param viewResolver 要应用属性的解析器
	 */
	public void applyToMvcViewResolver(Object viewResolver) {
		Assert.isInstanceOf(AbstractTemplateViewResolver.class, viewResolver,
				() -> "ViewResolver is not an instance of AbstractTemplateViewResolver :" + viewResolver);
		AbstractTemplateViewResolver resolver = (AbstractTemplateViewResolver) viewResolver;
		resolver.setPrefix(getPrefix());
		resolver.setSuffix(getSuffix());
		resolver.setCache(isCache());
		if (getContentType() != null) {
			resolver.setContentType(getContentType().toString());
		}
		resolver.setViewNames(getViewNames());
		resolver.setExposeRequestAttributes(isExposeRequestAttributes());
		resolver.setAllowRequestOverride(isAllowRequestOverride());
		resolver.setAllowSessionOverride(isAllowSessionOverride());
		resolver.setExposeSessionAttributes(isExposeSessionAttributes());
		resolver.setExposeSpringMacroHelpers(isExposeSpringMacroHelpers());
		resolver.setRequestContextAttribute(getRequestContextAttribute());
		// 该解析器通常作为后备解析器（例如 InternalResourceViewResolver），
		// 因此它需要较低的优先级
		resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 5);
	}

}
